package com.tchintech.pay.view.activity;

import android.support.v4.content.ContextCompat;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.tchintech.pay.R;
import com.tchintech.pay.delegate.PetroChinaDelegate;
import com.tchintech.pay.frame.presenter.ActivityPresenter;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/8/25 0025.
 * <p>
 * <p>
 * 中石油
 * 中石化
 * 加油
 */

public class PetroChinaActivity extends ActivityPresenter<PetroChinaDelegate> implements View.OnClickListener {
    private List<RelativeLayout> listReal;
    private List<EditText> listEdit;
    private List<String> listMonry;

    private List<Integer> listRealId;
    private List<Integer> listEditlId;
    private ImageView imageView;
    private int type;
    private String payMoney = "";
    private EditText et;

    @Override
    protected void initView() {
        super.initView();
        et = viewDelegate.get(R.id.et_6);
        imageView = viewDelegate.getIv_china();
        if (type == 1) {
            imageView.setImageDrawable(ContextCompat.getDrawable(this, R.drawable.sinopec_china));
            viewDelegate.setTv_title("中国石化");
        } else if (type == 2) {
            imageView.setImageDrawable(ContextCompat.getDrawable(this, R.drawable.petro_china));
            viewDelegate.setTv_title("中国石油");
        }

        viewDelegate.setOnClickListener(this, R.id.real_1, R.id.real_2, R.id.real_3, R.id.real_4,
                R.id.real_5, R.id.real_6, R.id.bt_submit, R.id.et_1, R.id.et_2, R.id.et_3, R.id.et_4, R.id.et_5, R.id.et_6, R.id.iv_back);
        editListener();
    }


    @Override
    protected void initData() {
        super.initData();
        type = getIntent().getIntExtra("type", 1);

        listRealId = new ArrayList<Integer>();
        listMonry = new ArrayList<String>();
        listReal = new ArrayList<RelativeLayout>();
        listEditlId = new ArrayList<Integer>();
        listEdit = new ArrayList<EditText>();
        listRealId.add(R.id.real_1);
        listRealId.add(R.id.real_2);
        listRealId.add(R.id.real_3);
        listRealId.add(R.id.real_4);
        listRealId.add(R.id.real_5);
        listRealId.add(R.id.real_6);

        listEditlId.add(R.id.et_1);
        listEditlId.add(R.id.et_2);
        listEditlId.add(R.id.et_3);
        listEditlId.add(R.id.et_4);
        listEditlId.add(R.id.et_5);
        listEditlId.add(R.id.et_6);
        for (int i = 0; i < listRealId.size(); i++) {
            listReal.add(viewDelegate.getReal(listRealId.get(i)));
        }
        for (int i = 0; i < listRealId.size(); i++) {
            listEdit.add(viewDelegate.getEdittex(listEditlId.get(i)));
        }
        String[] str = {"100", "300", "500", "1000", "3000"};

        for (int i = 0; i < str.length; i++) {
            listMonry.add(str[i]);
        }
    }

    @Override
    public void onClick(View v) {
        if (listReal.contains(v)) {
            for (int i = 0; i < listReal.size(); i++) {
                if (listReal.get(i) == v) {
                    changeReal(i);

                    break;
                }
            }
        } else if (v.getId() == R.id.bt_submit) {
            if (!payMoney .equals("")) {
                showMsg("缴费成功");
                finish();
            } else {
                showMsg("请选择金额");
            }
        } else if (listEdit.contains(v)) {
            for (int i = 0; i < listEdit.size(); i++) {
                if (listEdit.get(i) == v) {
                    changeReal(i);
                    break;
                }
            }

        } else if (v.getId() == R.id.iv_back) {
            finish();
        }


    }


    /**
     * 输入框监听，避免第一次点击输入框 整体不变色
     */
    private void editListener() {

        et.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    // 此处为得到焦点时的处理内容
                    changeReal(5);
                }
            }
        });
    }

    /**
     * g更改控件颜色
     *
     * @param num
     */
    private void changeReal(int num) {
        View view = listReal.get(num);
        for (int i = 0; i < listReal.size(); i++) {
            RelativeLayout rela = listReal.get(i);
            if (view == rela) {
                rela.setBackground(ContextCompat.getDrawable(this, R.drawable.recycle_pay_select));
            } else {
                rela.setBackground(ContextCompat.getDrawable(this, R.drawable.recycle_pay_bk));
            }
        }
        if (num != 5) {
            payMoney = listMonry.get(num);
        } else {
            payMoney = et.getText().toString().trim();
        }
    }


    @Override
    protected Class<PetroChinaDelegate> getDelegateClass() {
        return PetroChinaDelegate.class;
    }
}
